package com.ftg.learn.bus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ftg.learn.bus.entity.Approval;
import com.ftg.learn.bus.service.IApprovalService;
import com.ftg.learn.util.ResEntity;
import com.ftg.learn.util.ResPageEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 * 审批表 前端控制器
 * </p>
 *
 * @author lalala
 * @since 2022-03-07
 */
@RestController
@RequestMapping("/bus/approval")
@Api(tags = "审批管理功能列表")
public class ApprovalController extends CrudpController<Approval, IApprovalService> {


    @PutMapping("/updateApproval")
    @ApiOperation(value = "开始审批/二次审批")
    public synchronized ResEntity updateApproval(@RequestParam Long aid, @RequestParam Long beid,
                                                 @RequestParam int ispassed, @RequestParam String remarks) {
        return super.service.updateApp(aid, beid, ispassed, remarks) ? new ResEntity().OK() : new ResEntity().Failure();
    }

    @GetMapping("/pageOne")
    @ApiOperation(value = "需要审批/二次审批的分页展示")
    public ResPageEntity pageOne(@RequestParam int page, @RequestParam int limit, HttpServletRequest request){

        //获得当前请求里面所有参数 page=1 limit =10 uaccunt=a
        Map<String, String[]> map = request.getParameterMap();
        Set<Map.Entry<String, String[]>> keys = map.entrySet();

        IPage<Approval> ip = new Page<>(page,limit);
        QueryWrapper<Approval> qw = new QueryWrapper<>();
        for(Map.Entry<String, String[]> e:keys){
            if(!e.getKey().equals("page") && !e.getKey().equals("limit")
                    && e.getValue().length > 0 && !e.getValue()[0].equals("")){
                qw = (qw == null ? new QueryWrapper<>():qw);
                qw.like(e.getKey(),e.getValue()[0]);
            }
        }
        qw.eq("ispassed","1").or().eq("ispassed","2");
        service.page(ip,qw);


        return new ResPageEntity(ip);
    }

    @GetMapping("/pageTwo")
    @ApiOperation(value = "审批条目的分页展示")
    public ResPageEntity pageTwo(@RequestParam int page, @RequestParam int limit, HttpServletRequest request){
//        IPage<Approval> p = new Page<>(page,limit);
//        QueryWrapper<Approval> wrapper = new QueryWrapper<>();
//        wrapper.eq("ispassed","0");
//        service.page(p,wrapper);
//        return new ResPageEntity(p);

        //获得当前请求里面所有参数 page=1 limit =10 uaccunt=a
        Map<String, String[]> map = request.getParameterMap();
        Set<Map.Entry<String, String[]>> keys = map.entrySet();

        IPage<Approval> ip = new Page<>(page,limit);
        QueryWrapper<Approval> qw = new QueryWrapper<>();
        for(Map.Entry<String, String[]> e:keys){
            if(!e.getKey().equals("page") && !e.getKey().equals("limit")
                    && e.getValue().length > 0 && !e.getValue()[0].equals("")){
                qw = (qw == null ? new QueryWrapper<>():qw);
                qw.like(e.getKey(),e.getValue()[0]);
            }
        }
        qw.eq("ispassed","0");
        service.page(ip,qw);


        return new ResPageEntity(ip);
    }
}
